using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Hemi
{
    public partial class NewWorkItemsStage : UserControl, IMoveStage
    {
        MoveStageState _state = MoveStageState.OK;
        ProgressDialog _progressDialog;

        public NewWorkItemsStage()
        {
            InitializeComponent();

            _progressDialog = new ProgressDialog();
            _progressDialog.StartPosition = FormStartPosition.CenterParent;
        }

        #region IMoveStage Members

        void IMoveStage.CommitChanges()
        {           
        }

        string IMoveStage.Name
        {
            get { return "New Work Items"; }
        }

        bool IMoveStage.CanTestValidate
        {
            get { return false; }
        }

        string IMoveStage.Description
        {
            get { return @"Congratulations, you have moved your Work Items.  Your old and new Work Items are listed below.  Please use the File menu to do any additional moves."; }
        }

        string IMoveStage.InvalidMessage
        {
            get { return "New Work Items is invalid"; }
        }

        string IMoveStage.ValidMessage
        {
            get { return "Valid!"; }
        }

        MoveStageState IMoveStage.State
        {
            get { return _state; }
        }

        UserControl IMoveStage.UserControl
        {
            get { return this; }
        }

        bool IMoveStage.Validate()
        {
            return true;
        }

        #endregion

        private void NewWorkItemsStage_Load(object sender, EventArgs e)
        {
            _loadBackgroundWorker.RunWorkerAsync();
            _progressDialog.ShowDialog();
        }

        private void _loadBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //
            // Show the work items we are going to save
            //
            int totalNumber = HemiContextSingleton.Instance.WorkItemsToMove.Count;
            int oneIncrement = (1 / totalNumber) * 100;
            
            foreach (WorkItemMoveInfo workItem in HemiContextSingleton.Instance.WorkItemsToMove)
            {
                _loadBackgroundWorker.ReportProgress(oneIncrement, string.Format("Loading {0}", workItem.WorkItem.Title));
                C1.Win.C1FlexGrid.Row row = _workItemsFlex.Rows.Add();

                row["newId"] = workItem.Copy.Id;
                row["oldId"] = workItem.WorkItem.Id;
                row["title"] = workItem.Copy.Title;
                row["oldProject"] = workItem.WorkItem.Project.Name;
                row["newProject"] = workItem.Copy.Project.Name;
            }    
        }

        private void _loadBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SafeUpdateProgress(e);
        }

        private void _loadBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _progressDialog.Close();
        }

        private void SafeUpdateProgress(ProgressChangedEventArgs e)
        {
            _progressDialog._progressLabel.Text = e.UserState.ToString();

            int potentialUpdate = _progressDialog._progressBar.Value + e.ProgressPercentage;
            if (potentialUpdate < 100)
            {
                _progressDialog._progressBar.Value += e.ProgressPercentage;
            }
        }
    }
}
